package com.xudm.cdg.entity;

import lombok.Data;

import java.io.Serializable;

/**
 * @author zxw
 * 通用返回对象
 */
@Data
public class ResponseResult<T> implements Serializable {
    public static final long serialVersionUID = 42L;

    private Integer code;

    private String msg;

    private T data;

    protected ResponseResult() {

    }

    protected ResponseResult(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    protected ResponseResult(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    /**
     * 成功返回结果
     *
     * @param data 获取的数据
     */
    public static <T> ResponseResult<T> success(T data) {
        return new ResponseResult<T>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg(), data);
    }

    /**
     * 成功返回结果
     *
     * @param data 获取的数据
     * @param msg  提示信息
     */
    public static <T> ResponseResult<T> success(T data, String msg) {
        return new ResponseResult<>(ResultCode.SUCCESS.getCode(), msg, data);
    }

    /**
     * 成功返回结果
     *
     * @param msg 提示信息
     */
    public static <T> ResponseResult<T> success(String msg) {
        return new ResponseResult<>(ResultCode.SUCCESS.getCode(), msg);
    }

    /**
     * 失败返回结果
     *
     * @param errorCode 错误码
     */
    public static <T> ResponseResult<T> failed(IErrorCode errorCode) {
        return new ResponseResult<>(errorCode.getCode(), errorCode.getMsg(), null);
    }

    /**
     * 失败返回结果
     *
     * @param errorCode 错误码
     * @param msg       错误信息
     */
    public static <T> ResponseResult<T> failed(IErrorCode errorCode, String msg) {
        return new ResponseResult<>(errorCode.getCode(), msg, null);
    }

    /**
     * 失败返回结果
     *
     * @param msg 提示信息
     */
    public static <T> ResponseResult<T> failed(String msg) {
        return new ResponseResult<>(ResultCode.FAILED.getCode(), msg, null);
    }

    /**
     * 失败返回结果
     */
    public static <T> ResponseResult<T> failed() {
        return failed(ResultCode.FAILED);
    }

    /**
     * 未登录返回结果
     */
    public static <T> ResponseResult<T> unauthorized(T data) {
        return new ResponseResult<>(ResultCode.UNAUTHORIZED.getCode(), ResultCode.UNAUTHORIZED.getMsg(), data);
    }

    /**
     * 未授权返回结果
     */
    public static <T> ResponseResult<T> forbidden(T data) {
        return new ResponseResult<>(ResultCode.FORBIDDEN.getCode(), ResultCode.FORBIDDEN.getMsg(), data);
    }

    /**
     * 未授权返回结果
     */
    public static <T> ResponseResult<T> forbidden() {
        return forbidden(null);
    }

    /**
     * 请求类型或参数错误
     */
    public static <T> ResponseResult<T> badRequest(T data) {
        return new ResponseResult<>(ResultCode.BAD_REQUEST.getCode(), ResultCode.BAD_REQUEST.getMsg(), data);
    }

    /**
     * 请求类型或参数错误
     */
    public static <T> ResponseResult<T> badRequest() {
        return badRequest(null);
    }

    /**
     * 请求报文过大
     */
    public static <T> ResponseResult<T> requestTooLarge(T data) {
        return new ResponseResult<>(ResultCode.REQUEST_ENTITY_TOO_LARGE.getCode(), ResultCode.REQUEST_ENTITY_TOO_LARGE.getMsg(), data);
    }

    /**
     * 请求报文过大
     */
    public static <T> ResponseResult<T> unsupportedMediaType(T data) {
        return new ResponseResult<>(ResultCode.UNSUPPORTED_MEDIA_TYPE.getCode(), ResultCode.UNSUPPORTED_MEDIA_TYPE.getMsg(), data);
    }

    /**
     * 服务器未知错误
     */
    public static <T> ResponseResult<T> serverError(T data) {
        return new ResponseResult<>(ResultCode.SERVER_ERROR.getCode(), ResultCode.SERVER_ERROR.getMsg(), data);
    }
}
